package cn.wps.sdk.demo.util;

import java.io.File;
public class StringUtil {

    public StringUtil() {
    }

    public static String pathExtension(String fileName) {
        if (null == fileName) {
            return "";
        } else {
            int point = fileName.lastIndexOf(46);
            int separatorIndex = fileName.lastIndexOf(File.separator);
            if (point < separatorIndex) {
                return "";
            } else {
                int length = fileName.length();
                return point != -1 && point != length - 1 ? fileName.substring(point + 1, length) : "";
            }
        }
    }
    public static String getNamePart(String fileName) {
        if (null == fileName) {
            return "";
        } else {
            int point = getPathLastIndex(fileName);
            int length = fileName.length();
            if (point == -1) {
                return fileName;
            } else if (point == length - 1) {
                int secondPoint = getPathLastIndex(fileName, point - 1);
                if (secondPoint == -1) {
                    return length == 1 ? fileName : fileName.substring(0, point);
                } else {
                    return fileName.substring(secondPoint + 1, point);
                }
            } else {
                return fileName.substring(point + 1);
            }
        }
    }
    public static int getPathLastIndex(String fileName) {
        if (null == fileName) {
            return -1;
        } else {
            int point = fileName.lastIndexOf(47);
            if (point == -1) {
                point = fileName.lastIndexOf(92);
            }

            return point;
        }
    }

    public static int getPathLastIndex(String fileName, int fromIndex) {
        if (null == fileName) {
            return -1;
        } else {
            int point = fileName.lastIndexOf(47, fromIndex);
            if (point == -1) {
                point = fileName.lastIndexOf(92, fromIndex);
            }

            return point;
        }
    }
}

